<?php

namespace app\order\controller;

use app\common\service\ShunfengApiService;
use app\order\logic\OrderShipLogic;
use app\order\model\Order as OrderModel;
use Throwable;
use app\common\controller\PCBase;
use think\facade\Db;

class OrderShip extends PCBase
{
    /**
     * 模型
     * @var object
     */
    protected object $model;

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new OrderModel();
    }

    /**
     * 查看
     * @throws Throwable
     */
    public function index()
    {
        $params = $this->request->post();
        $orderShipLogic = new OrderShipLogic();
        return $orderShipLogic->getOrderShiopList($params);
    }



    /**
     * 异常订单自动发货
     */
    public function automaticShipping()
    {
        $params         = $this->request->post();
        $orderShipLogic = new OrderShipLogic();
        $status         = $params['status']??'';
        if($status==6){
            return  $orderShipLogic->automaticSplitShipping($params);
        }else{
            return  $orderShipLogic->automaticShipping($params);
        }

    }

    /**
     *  异常订单强制发货
     */
    public function mandatoryShipping()
    {
        $params = $this->request->post();
        $orderShipLogic = new OrderShipLogic();
        $status         = $params['status']??'';
        if($status == 6){
            return  $orderShipLogic->mandatorySplitShipping($params);
        }else{
            return  $orderShipLogic->mandatoryShipping($params);
        }
    }

    /**
     * 结束发货
     */
    public function endShipping()
    {
        $params = $this->request->post();
        $orderShipLogic = new OrderShipLogic();
        return $orderShipLogic->endShipping($params);
    }

    /**
     * 订单导出
     */
    public function export()
    {
        $params = $this->request->post();
        $orderShipLogic = new OrderShipLogic();
        return $orderShipLogic->exportOrder($params);
    }


    /**
     * @return void
     * @desc  异常重发详情接口
     */
    public function splitOrderInfo()
    {
        $params = $this->request->post();
        $id     = $params['id']??'';
        $status = $params['status']??'';
        if($status == 6){
            $row = (new OrderShipLogic())->splitOrderInfo($params);
        }else{
            $orders = Db::name('order')->where('id',$id)->field('id,order_id,shop_id,tracking_number')->find();
            $order_id        = $orders['order_id']??'';
            $tracking_number = $orders['tracking_number']??'';
            $shop_id         = $orders['shop_id']??'';
            $row      = Db::name('order_append')->where('order_id',$order_id)->find();

            unset($row['json']);
            $sku_info =  Db::name('order_info')->where('order_id', $order_id)->column('seller_sku');
            $sku_info =  $sku_info ? implode(',',$sku_info) : '';
            $row['skus_info'] = $sku_info;
            $row['shop_id']   = $shop_id;
            $row['tracking_number']   = $tracking_number;
        }
        return json(DefaultResponse(200, true, '获取成功',$row));
    }

    /**
     * @return void
     * @desc  异常重发编辑接口
     */
    public function editSplitOrder(){
        $params = $this->request->post();
        $status = $params['status']??'';
        htmlLog(['data'=>$params],OPERATE_LOG . date('Ym') . '/editSplitOrder/' . date('Ymd') . '.txt');
        if($status == 6){
            (new OrderShipLogic())->editSplitOrder($params);
        }else{
            (new OrderShipLogic())->editOrder($params);
        }
        return json(DefaultResponse(200, true, '操作成功', null));
    }



}
